package core.physics;

import java.util.Collection;
import java.util.LinkedList;

import core.Entity;

/**
 * Provides methods for detecting and reacting to Entity collisions.
 * 
 * @author Jason
 * 
 */
public class CollisionManager {

	private CollisionDetectionAlgorithm alg;

	public CollisionManager(CollisionDetectionAlgorithm algorithm) {
		setAlgorithm(algorithm);
	}

	/**
	 * Gets all collisions in the current state of the engine.
	 * 
	 * @param entities
	 *            The entities to search for collisions between.
	 */
	public Collection<Collision> getCollisions(Collection<Entity> entities) {
		LinkedList<Collision> collisions = new LinkedList<Collision>();
		for (Entity a : entities) {
			for (Entity b : entities) {
				if (a != b) {
					if (alg.isCollision(a, b))
						collisions.add(new Collision(a, b));
				}
			}
		}
		return collisions;
	}

	/**
	 * Sets the algorithm to use for collision detection.
	 * 
	 * @param algorithm
	 *            If this is null, the NoCollisionsAlgorithm is used.
	 */
	public void setAlgorithm(CollisionDetectionAlgorithm algorithm) {
		this.alg = algorithm;
		if (alg == null) {
			alg = new NoCollisionsAlgorithm();
		}
	}

	public CollisionDetectionAlgorithm getAlgorithm() {
		return alg;
	}

}
